package rabbitmq.work;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import rabbitmq.contant.Contants;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws Exception {
        // 1.建立连接
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(Contants.HOST);
        connectionFactory.setPort(Contants.PORT);
        connectionFactory.setUsername(Contants.USER_NAME);
        connectionFactory.setPassword(Contants.PASSWORD);
        connectionFactory.setVirtualHost(Contants.VIRTUAL_HOST);
        Connection connection = connectionFactory.newConnection();

        // 2.开启信道
        Channel channel = connection.createChannel();
        // 3.声明交换机 使用内置交换机
        channel.queueDeclare(Contants.WORK_QUEUE,true,false,false,null);
        // 4.发送消息
        for (int i=0;i<10;i++){
            String msg = "hello work queue.... "+i;
            channel.basicPublish("",Contants.WORK_QUEUE,null,msg.getBytes());
        }
        System.out.println("消息发送成功");
//        // 6.资源释放(有前后顺序)
//        channel.close(); // 可以省略
//        connection.close();

    }
}
